package com.ellricky.baselib.ui

import com.andr.common.ui.activity.BaseToolBarActivity
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.ToastUtil
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.dialog.picker.EPickerUtil
import com.ellricky.baselib.entity.new.StuDetail
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewECallback
import kotlinx.android.synthetic.main.abase_ctivity_student_edit.*
import kotlinx.android.synthetic.main.base_activity_stu_detail.*
import java.util.HashMap

class StudentEditActivity : BaseToolBarActivity<Void, BasePresenter<Void>>() {

    private val ePickerUtil: EPickerUtil by lazy {
        EPickerUtil(this)
    }
    private val memberId: String by lazy {
        intent.getStringExtra(BaseConstant.KEY_ID)
    }

    override fun getContentLayout(): Int {
        return R.layout.abase_ctivity_student_edit
    }

    override fun initUIView() {
        super.initUIView()
        mTvTitle.text = "编辑学生信息"

        loadDetail()

        tv_confirm.setOnClickListener {
            val name = et_name.text.toString()
            val sex = et_sex.text.toString()
            val birthday = tv_select.text.toString()
            val school = et_school.text.toString()
            val fatherJob = et_father_job.text.toString()
            val fatherPhone = et_father_phone.text.toString()
            val motherPhone = et_mother_phone.text.toString()
            val motherJob = et_mother_job.text.toString()
            val address = et_address.text.toString()

            val url = EAppUtil.getApiHost()
            val paramMap: MutableMap<String, String> = HashMap()
            paramMap["action"] = "modifyMemberInfo"
            paramMap["member_id"] = memberId
            paramMap["member_name"] = name
            paramMap["member_sex"] = sex
            paramMap["member_birthday"] = birthday
            paramMap["member_school"] = school
            paramMap["member_father_tel"] = fatherPhone
            paramMap["member_mother_tel"] = motherPhone
            paramMap["member_father_work"] = fatherJob
            paramMap["member_mother_work"] = motherJob
            paramMap["member_address"] = address
            paramMap["member_introduction_num"] = "0"
            paramMap["eid"] = LocalCacheUtil.getUid()

            HttpUtils.getInstance().postFrom(url, paramMap, object : NewECallback<Any?>(this) {
                override fun onSuccess(data: Any?, msg: String?) {
                    ToastUtil.show(this@StudentEditActivity, "修改成功")
                    finish()
                }
            })
        }

        tv_select.setOnClickListener {
            ePickerUtil.showBirthTimePicker("") { content, _, _ ->
                tv_select.text = content
            }
        }
    }

    private fun loadDetail() {
        val url = EAppUtil.getApiHost()
        val paramMap: MutableMap<String, String> = HashMap()
        paramMap["action"] = "studentDetail"
        paramMap["member_id"] = memberId
        paramMap["eid"] = LocalCacheUtil.getUid()

        HttpUtils.getInstance().postFrom(url, paramMap, object : NewECallback<StuDetail?>(this) {
            override fun onSuccess(data: StuDetail?, msg: String?) {
                data?.also {
                    et_name.setText(it.memeber_name)
                    et_sex.setText(it.memeber_sex)
                    tv_select.text = it.member_birthday
                    et_school.setText(it.member_school)
                    et_father_phone.setText(it.father_tel)
                    et_father_job.setText(it.father_work)
                    et_mother_phone.setText(it.mother_tel)
                    et_mother_job.setText(it.mother_work)
                    et_address.setText(it.address)
                }
            }
        })
    }
}